﻿using System;
using System.Text;

/// <summary>
/// 数据流解包器
/// </summary>
public class DPacket
{
    protected byte[] m_Buffer;      // 真实的数据缓冲区
    protected int m_iBufferLen;     // 需要解码的缓冲区大小
    protected int m_iRPos;          // 当前读指针的位置

    // 设置初始化缓冲区
    public DPacket(byte[] buffer, int iBufferLen)
    {
        m_Buffer = buffer;
        m_iBufferLen = iBufferLen;
        m_iRPos = 0;
    }

    // 得到内部缓冲区的首地址 
    public byte[] GetPacketBuffer()
    {
        return m_Buffer;
    }

    // 得到数据区的大小
    public int Size()
    {
        return m_iBufferLen;
    }

    // 得到当前读指针的位置
    public int GetReadPos()
    {
        return m_iRPos;
    }

    // 设置读指针的位置
    public void SetReadPos(int pos)
    {
        m_iRPos = pos;
    }

    // 清空数据
    public void Clear()
    {
        m_Buffer = null;
        m_iBufferLen = 0;
        m_iRPos = 0;
    }

    // 弹出Byte
    public byte ReadByte()
    {
        byte ret = m_Buffer[m_iRPos];
        m_iRPos++; return ret;
    }

    // 弹出Char
    public char ReadChar()
    {
        if (GetReadPos() + sizeof(char) > Size())
        {
            return '\0';
        }

        char ret = BitConverter.ToChar(m_Buffer, m_iRPos);
        m_iRPos += sizeof(char); return ret;
    }

    // 弹出bool
    public bool ReadBool()
    {
        if (GetReadPos() + sizeof(bool) > Size())
        {
            return false;
        }

        bool ret = BitConverter.ToBoolean(m_Buffer, m_iRPos);
        m_iRPos += sizeof(bool); return ret;
    }

    // 弹出Short
    public short ReadShort()
    {
        if (GetReadPos() + sizeof(short) > Size())
        {
            return 0;
        }

        short ret = BitConverter.ToInt16(m_Buffer, m_iRPos);
        m_iRPos += sizeof(short); return ret;
    }

    // 弹出Int
    public int ReadInt()
    {
        if (GetReadPos() + sizeof(int) > Size())
        {
            return 0;
        }

        int ret = BitConverter.ToInt32(m_Buffer, m_iRPos);
        m_iRPos += sizeof(int); return ret;
    }

    // 弹出Long
    public float ReadFloat()
    {
        if (GetReadPos() + sizeof(float) > Size())
        {
            return 0.0f;
        }

        float ret = BitConverter.ToSingle(m_Buffer, m_iRPos);
        m_iRPos += sizeof(float); return ret;
    }

    // 弹出Long
    public long ReadLong()
    {
        if (GetReadPos() + sizeof(long) > Size())
        {
            return 0;
        }

        long ret = BitConverter.ToInt64(m_Buffer, m_iRPos);
        m_iRPos += sizeof(long); return ret;
    }

    // 弹出指定长度的字符串(UTF8格式)
    public string ReadString()
    {
        int len = ReadInt();
        if (len <= 0)
        {
            return "";
        }

        if (GetReadPos() + len > Size())
        {
            return "";
        }

        string ret = Encoding.UTF8.GetString(m_Buffer, m_iRPos, len);
        m_iRPos += len; return ret;
    }

    // 弹出指定长度的字符串
    public byte[] ReadArray(int bufferLen)
    {
        byte[] ret = new byte[bufferLen + 1];
        for (int i = 0; i < bufferLen; i++)
        {
            ret[i] = m_Buffer[m_iRPos];
            m_iRPos++;
        }
        ret[bufferLen] = 0;
        return ret;
    }
}